package com.smartleanx.module.hrm.controller.admin.programme;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.excel.core.util.ExcelUtils;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.dal.dataobject.programme.*;
import com.smartleanx.module.hrm.dal.dataobject.programme.bo.*;
import com.smartleanx.module.hrm.dal.dataobject.programme.vo.HrmProgrammeUpdateVo;
import com.smartleanx.module.hrm.dal.dataobject.programme.vo.ProgrammeEmployeeVo;
import com.smartleanx.module.hrm.dal.dataobject.programme.vo.SystemChinaAreaVo;
import com.smartleanx.module.hrm.service.programme.HrmProgrammeService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/hrm/programme")
public class ProgrammeController {
    @Resource
    public HrmProgrammeService hrmProgrammeService;

    @GetMapping("/areaBySelect")
    public CommonResult<List<SystemChinaAreaVo>> areaBySelect() {
        List<SystemChinaAreaVo> systemChinaAreaVos = hrmProgrammeService.areaBySelect();
        return CommonResult.success(systemChinaAreaVos);
    }

    @GetMapping("/insuranceBySelect")
    public CommonResult<List<JSONObject>> insuranceBySelect(@RequestParam(value = "name", required = false) String name, @RequestParam("type") Integer type) {
        List<JSONObject> jsonObjects = hrmProgrammeService.insuranceBySelect(name, type,1);
        return CommonResult.success(jsonObjects);
    }
    @GetMapping("/insuranceBySelectUpdate")
    public CommonResult<List<JSONObject>> insuranceBySelectUpdate(@RequestParam(value = "name", required = false) String name, @RequestParam("type") Integer type) {
        List<JSONObject> jsonObjects = hrmProgrammeService.insuranceBySelect(name, type,null);
        return CommonResult.success(jsonObjects);
    }

    @GetMapping("/roundUpBySelect")
    public CommonResult<List<JSONObject>> roundUpBySelect(@RequestParam(value = "name", required = false) String name) {
        List<JSONObject> jsonObjects = hrmProgrammeService.roundUpBySelect(name);
        return CommonResult.success(jsonObjects);
    }

    @GetMapping("/accountBySelect")
    public CommonResult<List<JSONObject>> accountBySelect(@RequestParam(value = "name", required = false) String name) {
        List<JSONObject> jsonObjects = hrmProgrammeService.accountBySelect(name);
        return CommonResult.success(jsonObjects);
    }

    @PostMapping("/programmeSave")
    public CommonResult<Object> programmeSave(@RequestBody HrmProgrammeSaveBo hrmProgrammeSaveBo) {
        Integer i = hrmProgrammeService.programmeSave(hrmProgrammeSaveBo);
        return CommonResult.success(i);
    }



    @GetMapping("/selectInsertHX")
    public CommonResult<HrmProgrammeUpdateVo> selectInsertHX() {
        HrmProgrammeUpdateVo hrmProgrammeUpdateVo = hrmProgrammeService.selectInsertHX();
        return CommonResult.success(hrmProgrammeUpdateVo);
    }

    @GetMapping("/selectProgramme")
    public CommonResult<PageInfo<HrmProgramme>> selectProgramme(@RequestParam(value = "cityId", required = false) Integer cityId, @RequestParam(value = "programmeName", required = false) String programmeName, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<HrmProgramme> hrmProgrammePageInfo = hrmProgrammeService.selectProgramme(cityId, programmeName, pageNum, pageSize);
        return CommonResult.success(hrmProgrammePageInfo);
    }

    @GetMapping("/removeProgramme")
    public CommonResult<Object> removeProgramme(@RequestParam(value = "id")Integer id)
    {
        hrmProgrammeService.removeProgramme(id);
        return CommonResult.success("操作成功");
    }
    @GetMapping("/updateStatusProgramme")
    public CommonResult<Object> updateStatusProgramme(@RequestParam(value = "id")Integer id,@RequestParam(value = "state")Integer state){
        hrmProgrammeService.updateState(id,state);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/HrmProgrammeUpdateVo")
    public CommonResult<HrmProgrammeUpdateVo> selectProgrammeHX(Integer id) {
        HrmProgrammeUpdateVo hrmProgrammeUpdateVo = hrmProgrammeService.selectProgrammeHX(id);
        return CommonResult.success(hrmProgrammeUpdateVo);
    }

    @PostMapping("/programmeUpdate")
    public CommonResult<Object> programmeUpdate(@RequestBody HrmProgrammeSaveBo hrmProgrammeSaveBo) {
//        hrmProgrammeSaveBo.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
        Integer i = hrmProgrammeService.programmeUpdate(hrmProgrammeSaveBo);
        return CommonResult.success(i);
    }

    @PostMapping("/selectProgrammeEmployeeBySelect")
    public CommonResult<Object> selectProgrammeEmployeeBySelect(@RequestBody ProgrammeEmployeeBo programmeEmployeeBo) {
        PageInfo<ProgrammeEmployeeVo> programmeEmployeeVoPageInfo = hrmProgrammeService.selectProgrammeEmployeeBySelect(programmeEmployeeBo);
        return CommonResult.success(programmeEmployeeVoPageInfo);
    }

    @PostMapping("/insertProgrammeEmployeeStop")
    public CommonResult<Object> insertProgrammeEmployeeStop(@RequestBody ProgrammeEmployeeSaveBo programmeEmployeeSaveBo) {
        Integer i = hrmProgrammeService.insertProgrammeEmployeeStop(programmeEmployeeSaveBo);
        return CommonResult.success(i);
    }

    @PostMapping("/insertProgrammeEmployee")
    public CommonResult<Object> insertProgrammeEmployee(@RequestBody ProgrammeEmployeeSaveBo programmeEmployeeSaveBo) {
        hrmProgrammeService.insertProgrammeEmployee(programmeEmployeeSaveBo);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/selectProgrammeEmployeeInfoListById")
    public CommonResult<Object> selectProgrammeEmployeeInfoListById(@RequestParam("employeeId") Integer employeeId) {

        JSONObject jsonObject = hrmProgrammeService.selectProgrammeEmployeeInfoListById(employeeId);
        return CommonResult.success(jsonObject);
    }


    @GetMapping("/selectCompanySelect")
    public CommonResult<Object> selectCompanySelect(@RequestParam(value = "companyName", required = false) String companyName) {
        List<JSONObject> jsonObjects = hrmProgrammeService.selectCompanySelect(companyName);
        return CommonResult.success(jsonObjects);
    }

    @PostMapping("/importProgrammeEmployee")
    public CommonResult<Object> importProgrammeEmployee(@RequestParam("file") MultipartFile file) throws Exception {
        List<ProgrammeEmployeeSaveImportBo> list = ExcelUtils.read(file, ProgrammeEmployeeSaveImportBo.class, 1);
        JSONObject s = hrmProgrammeService.importProgrammeEmployee(list);
        return CommonResult.success(s);
    }

    /**
     * 公司
     */
    @GetMapping("/selectCompanyPageList")
    public CommonResult<Object> selectPageList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "companyName", required = false) String companyName) {
        PageInfo<HrmProgrammeCompany> hrmProgrammeCompanyPageInfo = hrmProgrammeService.selectPageList(pageNum, pageSize, companyName);
        return CommonResult.success(hrmProgrammeCompanyPageInfo);

    }


    @PostMapping("/insertCompany")
    public CommonResult<Object> insertCompany(@RequestBody HrmProgrammeCompany hrmProgrammeCompany) {
        Integer i = hrmProgrammeService.insertCompany(hrmProgrammeCompany);
        return CommonResult.success(i);

    }


    @PostMapping("/updateCompany")
    public CommonResult<Object> updateCompany(@RequestBody HrmProgrammeCompany hrmProgrammeCompany) {
        Integer i = hrmProgrammeService.updateCompany(hrmProgrammeCompany);
        return CommonResult.success(i);

    }


    @PostMapping("/updateState")
    public CommonResult<Object> updateState(@RequestBody HrmProgrammeCompany hrmProgrammeCompany) {
        Integer i = hrmProgrammeService.updateState(hrmProgrammeCompany);
        return CommonResult.success(i);

    }


    @GetMapping("/removeCompany/{id}")
    public CommonResult<Object> removeCompany(@PathVariable("id") Integer id) {
        Integer i = hrmProgrammeService.removeCompany(id);
        return CommonResult.success(i);

    }

    /**
     * 账户
     */
    @GetMapping("/selectAccountPageList")
    public CommonResult<Object> selectAccountPageList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "accountName", required = false) String accountName) {
        PageInfo<HrmProgrammeAccount> hrmProgrammeAccountPageInfo = hrmProgrammeService.selectAccountPageList(pageNum, pageSize, accountName);
        return CommonResult.success(hrmProgrammeAccountPageInfo);

    }

    @PostMapping("/insertAccount")
    public CommonResult<Object> insertAccount(@RequestBody HrmProgrammeAccount hrmProgrammeAccount) {
        Integer i = hrmProgrammeService.insertAccount(hrmProgrammeAccount);
        return CommonResult.success(i);

    }

    @PostMapping("/updateAccount")
    public CommonResult<Object> updateAccount(@RequestBody HrmProgrammeAccount hrmProgrammeAccount) {
        Integer i = hrmProgrammeService.updateAccount(hrmProgrammeAccount);
        return CommonResult.success(i);

    }


    @PostMapping("/updateAccountState")
    public CommonResult<Object> updateAccountState(@RequestBody HrmProgrammeAccount hrmProgrammeAccount) {
        Integer i = hrmProgrammeService.updateAccountState(hrmProgrammeAccount);
        return CommonResult.success(i);

    }

    @GetMapping("/removeAccount/{id}")
    public CommonResult<Object> removeAccount(@PathVariable("id") Integer id) {
        Integer i = hrmProgrammeService.removeAccount(id);
        return CommonResult.success(i);

    }

    /**
     * 险种
     *
     * @param
     * @return
     */
    @GetMapping("/selectInsurancePageList")
    public CommonResult<Object> selectAccountPageList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("name") String name, @RequestParam("type") Integer type) {
        PageInfo<HrmProgrammeInsurance> hrmProgrammeInsurancePageInfo = hrmProgrammeService.selectInsurancePageList(pageNum, pageSize, name, type);
        return CommonResult.success(hrmProgrammeInsurancePageInfo);

    }

    @PostMapping("/insertInsurance")
    public CommonResult<Object> insertInsurance(@RequestBody HrmProgrammeInsurance hrmProgrammeInsurance) {
        Integer i = hrmProgrammeService.insertInsurance(hrmProgrammeInsurance);
        return CommonResult.success(i);

    }

    @PostMapping("/updateInsurance")
    public CommonResult<Object> updateAccount(@RequestBody HrmProgrammeInsurance hrmProgrammeInsurance) {
        Integer i = hrmProgrammeService.updateInsurance(hrmProgrammeInsurance);
        return CommonResult.success(i);

    }


    @PostMapping("/updateInsuranceState")
    public CommonResult<Object> updateAccountState(@RequestBody HrmProgrammeInsurance hrmProgrammeInsurance) {
        Integer i = hrmProgrammeService.updateInsuranceState(hrmProgrammeInsurance);
        return CommonResult.success(i);

    }

    @GetMapping("/removeInsurance/{id}")
    public CommonResult<Object> removeInsurance(@PathVariable("id") Integer id) {
        Integer i = hrmProgrammeService.removeInsurance(id);
        return CommonResult.success(i);

    }


    @GetMapping("/selectProgrammeEmployeeInfoById")
    public CommonResult<Object> selectProgrammeEmployeeInfoById(@RequestParam("programmeId") Integer programmeId) {
        HrmProgrammeEmployee hrmProgrammeEmployee = hrmProgrammeService.selectProgrammeEmployeeInfoById(programmeId);
        return CommonResult.success(hrmProgrammeEmployee);
    }

    @PostMapping("/programmeEmployeeInfoUpdateById")
    public CommonResult<Object> programmeEmployeeInfoUpdateById(@RequestBody HrmProgrammeEmployeeUpdateBo hrmProgrammeEmployeeUpdateBo) {
        Integer i = hrmProgrammeService.programmeEmployeeInfoUpdateById(hrmProgrammeEmployeeUpdateBo);
        return CommonResult.success(i);

    }

    @PostMapping("/programmeEmployeeInfoCx")
    public CommonResult<Object> programmeEmployeeInfoCx(@RequestBody HrmProgrammeEmployeeUpdateBo hrmProgrammeEmployeeUpdateBo) {
        Integer i = hrmProgrammeService.programmeEmployeeInfoCx(hrmProgrammeEmployeeUpdateBo);
        return CommonResult.success(i);
    }

    @GetMapping("/selectByEmployee")
    public CommonResult<Object> selectByEmployee(@RequestParam("employeeId") Integer employeeId) {
        JSONObject jsonObject = hrmProgrammeService.selectByEmployee(employeeId);
        return CommonResult.success(jsonObject);
    }


    @GetMapping("/selectGRHJAmount")
    public CommonResult<Object> selectGRHJAmount(@RequestParam("year") String year) {
        List<JSONObject> jsonObjectList = hrmProgrammeService.selectGRHJAmount(year);
        return CommonResult.success(jsonObjectList);
    }

    @GetMapping("/selectAccountCompanyBill")
    public CommonResult<Object> selectAccountCompanyBill(@RequestParam("month") String month, @RequestParam(value = "companyName", required = false) String companyName, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<JSONObject> jsonObjectList = hrmProgrammeService.selectAccountCompanyBill(month, companyName, pageNum, pageSize);
        return CommonResult.success(jsonObjectList);
    }

    @GetMapping("/selectZDByEmployeeBill")
    public CommonResult<Object> selectZDByEmployeeBill(@RequestParam("month") String month, @RequestParam(value = "name", required = false) String name, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<JSONObject> jsonObjectList = hrmProgrammeService.selectZDByEmployeeBill(month, name, pageNum, pageSize);
        return CommonResult.success(jsonObjectList);
    }

    @GetMapping("/selectZDByEmployeeAndCompany")
    public CommonResult<Object> selectZDByEmployeeAndCompany(@RequestParam("month") String month, @RequestParam(value = "name", required = false) String name, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("companyId") Integer companyId) {
        PageInfo<JSONObject> jsonObjectList = hrmProgrammeService.selectZDByEmployeeBill(month, name, pageNum, pageSize);
        return CommonResult.success(jsonObjectList);
    }


    @GetMapping("/selectByEmployeeIdZdMX")
    public CommonResult<Object> selectByEmployeeIdZdMX(@RequestParam("employeeId") Integer employeeId, @RequestParam("month") String month) {
        Map jsonObject = hrmProgrammeService.selectByEmployeeIdZdMX(employeeId, month);
        return CommonResult.success(jsonObject);
    }


    @GetMapping("/getRemark")
    public CommonResult<Object> getRemark(@RequestParam("id") Integer id) {
        String str = hrmProgrammeService.getRemark(id);
        return CommonResult.success(str);
    }

    @PostMapping("/updateEmployeeZdMX")
    public CommonResult<Object> updateEmployeeZdMX(@RequestBody JSONObject jsonObject) {
        hrmProgrammeService.updateEmployeeZdMX(jsonObject);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/updateRemark")
    public CommonResult<Object> updateRemark(@RequestParam("id") Integer id, @RequestParam("remark") String remark) {

        hrmProgrammeService.updateRemark(id, remark);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/selectProgrammeZDAmount")
    public CommonResult<Object> selectProgrammeZDAmount(@RequestParam("id") Integer id, @RequestParam("type") Integer type) {
        List<JSONObject> jsonObjects = hrmProgrammeService.selectProgrammeZDAmount(id, type);
        return CommonResult.success(jsonObjects);
    }

    @GetMapping("/programmeBySelect/{cityId}")
    public CommonResult<Object> programmeBySelect(@PathVariable("cityId") Integer cityId) {
        List<JSONObject> jsonObjectList = hrmProgrammeService.programmeBySelect(cityId);
        return CommonResult.success(jsonObjectList);
    }

    @GetMapping("/renew")
    public CommonResult<Object> renew(@RequestParam("id") Integer id, @RequestParam("month") String month) {
        hrmProgrammeService.generateRecord(id, month);
        return CommonResult.success("更新成功");
    }

    @GetMapping("/generateRecord")
    public CommonResult<Object> generateRecord(Integer employeeId, String month) {
        hrmProgrammeService.generateRecord(employeeId, month);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入参保员工模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<ProgrammeEmployeeSaveImportBo> list = Arrays.asList(
                ProgrammeEmployeeSaveImportBo.builder().userName("yunai").companyName("1111").isProgramme("yunai@leanmaster.cn").programmeName("15601691300")
                        .accountName("精益大师").build(),
                ProgrammeEmployeeSaveImportBo.builder().userName("yuanma").companyName("1111").isProgramme("yuanma@leanmaster.cn").programmeName("15601701300")
                        .accountName("源码").build()
        );
        // 输出
        ExcelUtils.write(response, "用户导入模板.xlsx", "用户列表", ProgrammeEmployeeSaveImportBo.class, list);
    }
}
